#/usr/bin/python3
#coding=utf8

import base64,requests,time,hashlib,json,random




def create_safe(time):
	base64_before = base64.b64encode(('9622'+time).encode());
	return hashlib.md5(base64_before).hexdigest()


# def spider_python_spider(url,headers):
# 	return requests.request('GET',url,headers)




if __name__ == '__main__':
	url = 'https://www.python-spider.com/api/challenge6'
	


	try:
		total_title_number = 0
		exist_zhaobiao = int(0)
		# txt_path = r'D:\TestCode\self_spider\1.txt'
		# f = open(txt_path,mode='a',encoding='utf-8')

		headers = {
			# 'timestamp':timestamp,
			# 'safe':create_safe(timestamp)
			# 'sign': '"1629473538~YWlkaW5nX3dpbjE2Mjk0NzM1MzgyODA=|b23b92ea95d75468e19e46*"'
			'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0',
			'Host':'www.python-spider.com',
			'X-Requested-With':'XMLHttpRequest',
			'Origin':'https://www.python-spider.com',
			'Referer':'https://www.python-spider.com/challenge/7',
			'Sec-Fetch-Dest':'empty',
			'Sec-Fetch-Mode':'cors',
			'Sec-Fetch-Site':'same-origin',
		  	'Accept': 'application/json, text/javascript, */*; q=0.01',
		  	'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
	  		'Accept-Encoding':'gzip, deflate',
			'Cache-Control': 'max-age=0',
		}
		# old_sign = 'sign="1629473538~YWlkaW5nX3dpbjE2Mjk0NzM1MzgyODA=|b23b9*"'
		#
		# cookies = {
		# 	'sessionid': 'iqj9bjk6tf15k81h63ngv70ffsdi0t5n',
		# 	'vaptchaNetway': 'cn',
		# 	'Hm_lvt_337e99a01a907a08d00bed4a1a52e35d': '1629558451,1629641155',
		# 	'no-alert': 'true',
		# 	'Hm_lpvt_337e99a01a907a08d00bed4a1a52e35d': '1629641214',
		# }
		# res = requests.request('POST', url, headers=headers, cookies=cookies, data={'page':1}, timeout=20)
		#
		# # new_cookie = res.headers['set-cookie'].replace('; Path=/', '').replace('sign="sign=\\"', '"')
		# #print(res.headers['set-cookie'].replace('; Path=/', '').replace('sign=',''))
		# new_cookie = '"'+res.headers['set-cookie'].replace('; Path=/', '').replace('sign=','')+'"'

		for j in range(1,101):
			data = {
				'page':j,
			}
			# timestamp = str(int(time.time()))
			# time.sleep(random.randint(1,15)) #随机从1.到15之间出现一个数
			time.sleep(random.random())
			# cookies={
			# 	'sessionid':'215i73rjo9oo005uw2jdv5kbcygmbbcw',
			# 	'vaptchaNetway': 'cn',
			# 	'Hm_lvt_337e99a01a907a08d00bed4a1a52e35d': '1629298800,1629460306',
			# 	'no-alert': 'true',
			# 	'Hm_lpvt_337e99a01a907a08d00bed4a1a52e35d':'1629561901',
			# 	'sign': new_cookie
			# }
			cookies = {
				'sessionid': 'iqj9bjk6tf15k81h63ngv70ffsdi0t5n',
				'vaptchaNetway': 'cn',
				'Hm_lvt_337e99a01a907a08d00bed4a1a52e35d': '1629558451,1629641155',
				'no-alert': 'true',
				'Hm_lpvt_337e99a01a907a08d00bed4a1a52e35d': '1629641214',
			}

			res =  requests.request('POST',url,headers=headers,cookies=cookies,data=data,timeout=20)
			print(headers,cookies,data,res.headers,requests.utils.dict_from_cookiejar(res.cookies))
			print("--------------{%d}-------------" % j)
			jsonstrdata = json.loads(res.content)


			# print(type(jsonstrdata))
			if 'data' in jsonstrdata:
				response_data = jsonstrdata['data']

				for i in response_data:
					# total_title_number=total_title_number+1
					# print(i)
					print(type(i['value']))
					print(i['value'])
					# print(i['value'].strip())
					if type(i['value']) == str:
						number = int(i['value'].strip())
					#else:
						#number = i['value']



					exist_zhaobiao=exist_zhaobiao+ number
					# if i['message'].find('招') != -1:
					# 	exist_zhaobiao=exist_zhaobiao+int(1)
			else:
				print(jsonstrdata)
				# f.write(i['message']+'\n')
					# print('total_title_number: ',total_title_number)	
				
					# print('exist_zhaobiao: ',exist_zhaobiao)

		print('---------------------------------------------------总的------------------------------------')
		# f.close()
		print(exist_zhaobiao)
		# print(total_title_number)

	except Exception as e:
		print('errors')
		print(e)
		print('errors')
	

	# # current_time  = str(time.time())
	# current_time = str('1629260133')

	# print(create_safe(current_time))
